using System;
using System.Collections.Generic;
using pub.Settings;

namespace pub.MRU
{
    public class MruPersister : IMruPersister
    {
        private ISettings settings;

        public MruPersister(ISettings settings)
        {
            this.settings = settings;
        }

        public IList<string> Load(int maxCount)
        {
            var items = new List<string>();
            var sectionValues = settings.Entries;
            for (var n = 0; n < maxCount; n++)
            {
                var key = "Item" + (n + 1);
                if (!sectionValues.ContainsKey(key))
                {
                    break;
                }
                items.Add(sectionValues[key]);
            }
            return items;
        }

        public void Save(IList<string> items, int maxCount)
        {
            settings.Clear();
            var count = Math.Min(items.Count, maxCount);
            for (var n = 0; n < count; n++)
            {
                settings["Item" + (n + 1)].StringValue = items[n];
            }
        }
    }
}